package com.todoapp.page;

import java.util.Date;

import org.apache.wicket.PageParameters;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import todo.core.bso.TodoService;
import todo.core.domain.Todo;


/**
 * NewTodo WebPage
 * 
 */
public class EditTodo extends Template {
	
	private boolean newEntity = true;
	
	/**
	 * InputForm [inner class of NewTodo]
	 */
	private class InputForm extends Form {
		
		/**
		 * InputForm constructor
		 * @param name
		 * @param model
		 */
		public InputForm(String name, IModel<Todo> model) {
			super(name, model);
			
			DateTextField dateTextField = new DateTextField("dateTextField", new PropertyModel<Date>(
					model, "dueDt"), new StyleDateConverter("S-", true));
			
			add(new RequiredTextField<String>("title"));
			add(new TextArea<String>("description"));
			add(dateTextField);
			dateTextField.add(new DatePicker());
			add(new CheckBox("complete"));
			
			// save button
			add(new Button("saveButton") {
				@Override
				public void onSubmit()
				{
					// oddly enough, the ModelObject is still null at this phase
					System.out.println("saveButton -> getModelObject()=" + getModelObject());					
					setResponsePage(HomePage.class);
				}
			}.setDefaultFormProcessing(true));
			
			
			// cancel button
			add(new Button("cancelButton") {
				@Override
				public void onSubmit()
				{
					setResponsePage(HomePage.class);
				}
			}.setDefaultFormProcessing(false));
		}
		
		@Override
		protected void onSubmit() {
			// this method goes not get called if Form->Button.DefaultFormProcessing == false
			super.onSubmit();
			System.out.println("onSubmit() -> getModelObject()=" + getModelObject());
			Todo todo = (Todo) getDefaultModelObject();
			if (isNewEntity()) {
				getTodoService().save(todo);
			} else {
				getTodoService().update(todo);
			}
		}
		
	} // end of inner-class definition
	
	/**
	 * Default constructor
	 */
	public EditTodo() {
		this(new Todo());
		setNewEntity(true);
	}
	/**
	 * Default constructor
	 */
	public EditTodo(Todo todo) {
		
		setNewEntity(false);
		CompoundPropertyModel<Todo> cpModel = new CompoundPropertyModel<Todo>(todo);
		Form form = new InputForm("inputForm",cpModel); 

		add(form);
	}	
	/**
	 * No-session constructor
	 * @param params
	 */
	public EditTodo(PageParameters params) {
		this();
		System.out.println("Contstructing EditTodo(PageParameters params)");
	}
	@Override
	public String getPageTitle() {
		return isNewEntity()? "Create New Todo":"Edit Todo";
	}
	public boolean isNewEntity() {
		return newEntity;
	}
	public void setNewEntity(boolean newEntity) {
		this.newEntity = newEntity;
	}

}
